﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Business;
using Model;
using System.Web.Security;
using MB.Util;
using Public;

namespace MvcAppWeb.Controllers
{
    public class VenderUserLicenseController : BaseDataController
    {
        //
        // GET: /VenderUserLicense/

        public ActionResult VenderUserLicense()
        {
            if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            else
            {
                VenderUser model = (VenderUser)Session["UserInfo"];
                return View(model);
            }
        }

        public JsonResult LicenseTypeList(bool IsSetAll)
        {
            var retData = new object();
            retData = new SysEnum().GetEnumList(typeof(SysEnum.LicenseType), IsSetAll, false);
            return Json(retData);
        }

        public JsonResult Query(string LICENSE_TYPE)
        {
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            int total = 0;
            VenderUser model = (VenderUser)Session["UserInfo"];
            string where = " where 1=1";

            //管理员测试数据放开
            if (model.USERCODE != "system")
            {
                where += " and vl.venderid=" + model.VENDERID;
            }
            if (!string.IsNullOrEmpty(LICENSE_TYPE) && LICENSE_TYPE != "-1")
            {
                where += " and vl.LICENSE_TYPE='" + LICENSE_TYPE + "'";
            }
            //if (CheckTimeS != null)
            //{
            //    where += " and vl.VALIDDATE>= to_date('" + CheckTimeS + "','yyyy-mm-dd hh24:mi:ss')";
            //}
            //if (CheckTimeE != null)
            //{
            //    where += " and vl.VALIDDATE<= to_date('" + CheckTimeE + "','yyyy-mm-dd hh24:mi:ss')";
            //}
            where += " order by vl.VALIDDATE DESC";
            BVenderUserLicense bvl = new BVenderUserLicense();
            var result = bvl.GetVenderUserLicenseList(page, rows, out total, where);

            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", total);
            json.Add("rows", result);
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        
        public string Saves()
        {
            string sjson = string.Empty;
            try
            {
                string ValidDate = Request["ValidDate"];
                string LICENSE_TYPE = Request["LICENSE_TYPE"];
                string Remark = Request["txtRemark"];
                string ImgURL = Request["ImgURL"];
                string ImgName = Request["ImgName"];
                int vid = int.Parse(Request["VENDERID"]);
                VenderUser_License _VenderUser_License = new VenderUser_License();
                _VenderUser_License.LICENSE_TYPE = int.Parse(LICENSE_TYPE);
                _VenderUser_License.VALIDDATE = DateTime.Parse(ValidDate);
                _VenderUser_License.VENDERID = vid;
                _VenderUser_License.REMARK = Remark;
                VenderUser model = (VenderUser)Session["UserInfo"];
                _VenderUser_License.CREATOR = model.USERNAME;
                _VenderUser_License.CREATEDATE = DateTime.Now;
                _VenderUser_License.EDITOR = model.USERNAME;
                _VenderUser_License.EDITDATE = DateTime.Now;
                _VenderUser_License.IMGURL = ImgURL;
                _VenderUser_License.IMGNAME = ImgName;
                BVenderUserLicense bvl = new BVenderUserLicense();
                bvl.InsertVenderUserLicense(_VenderUser_License);
                sjson = "{success:true}";
                //NewCommon nc = new NewCommon();
                //VenderUser checkuser = nc.CheckVenderUser(code);
                //if (checkuser != null)
                //{
                //    sjson = "{success:false,msg:'该登录名已存在！'}";
                //}
                //else
                //{
                //    nc.AddVenderUser(user);
                //    sjson = "{success:true}";
                //}
            }
            catch (Exception ex)
            {
                sjson = "{success:false,msg:" + ex.Message + "}";
            }
            return sjson;
        }

        [HttpPost]
        public string Delete(string ids)
        {
            string sjson = string.Empty;
            try
            {
                BVenderUserLicense bvl = new BVenderUserLicense();
                foreach (var id in ids.Split(','))
                {
                    bvl.DeleteVenderUserLicense(id);
                }
                sjson = "{success:true}";
            }
            catch (Exception ex)
            {
                sjson = "{success:false,msg:" + ex.Message + "}";
            }
            return sjson;

        }

    }
}
